package com.module.product;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import base.util.Page;
import base.util.StringUtil;

import com.module.organization.OrganizationService;
import com.module.product.manager.EntProductManager;
import com.module.product.manager.ProductManager;
import com.module.product.vo.AttrItem;
import com.module.product.vo.EntProduct;
import com.module.product.vo.Product;
import com.module.sys.LoginSession;

public class ProductService {
	EntProductManager entProductManager = EntProductManager.getInstance();
	ProductManager productManager = ProductManager.getInstance();
	public Page list(EntProduct obj, Integer start, Integer limit)
			throws Exception {
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		return entProductManager.getList(obj, page);
	}
	/**
	 * 正式表查询
	 * @param obj
	 * @param start
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	public Page productList(EntProduct obj, Integer start, Integer limit)
			throws Exception {
		Page page = new Page();
		Product p = new Product();
		PropertyUtils.copyProperties(p,obj);
		
		page.setStart(start);
		page.setLimit(limit);
		return productManager.getList(p, page);
	}
	/**
	 * 根据ID查询正式表数据
	 * @param id
	 * @return
	 */
	public EntProduct getProduct(String id) {
		try {
			
			Product p =  productManager.get(id);
			EntProduct  ep = new EntProduct();
			PropertyUtils.copyProperties(ep,p);
			return ep;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	public EntProduct findbyid(String id) {
		try {

			return entProductManager.getEntProduct(id);

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
	
	public List listByGroupId(String groupId,String productId){
		try {
		AttrItem obj = new AttrItem();
		obj.setAttrGroup(groupId);
		obj.setProductId(productId);
		return entProductManager.listByGroupId(obj);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public EntProduct findByName(EntProduct obj) {
		try {
			return entProductManager.findByName(obj);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Map save(EntProduct obj) {
		try {
			if (StringUtils.isBlank(obj.getId()))// 添加
			{
				if (StringUtil.isNull(obj.getOrgId())) {
					obj.setOrgId(LoginSession.getOrgid());
					obj.setOrgName(LoginSession.getOrgname());
					obj.setCreaterId(LoginSession.getUserid());
				}

			} else// 修改
			{
				obj.setUpdaterId(LoginSession.getUserid());
			}
			return entProductManager.saveProductAndAttr(obj);
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public Map Audit_Product(EntProduct obj) {
		try {
				obj.setApprovePersonId(LoginSession.getUserid());
				obj.setApprovePersonName(LoginSession.getUsername());
				obj.setApproveTime(new Date());
			    return entProductManager.Audit(obj);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public Map<String,Object> saveImport(List<EntProduct> list) throws Exception{
		/*
		isTrue = entProductManager.importProduct(beanUnit.getProductList()); 
		 * */
		Map<String,Object> map = new HashMap<String,Object>();
		boolean isTrue=entProductManager.importProduct(list);
		map.put("state", isTrue);
		if (isTrue == true) {
			map.put("msg", "保存成功");
		} else {
			map.put("msg", "保存失败");
		}
		return map;
	}
	
	
	/**
	 * 批量提交审核
	 * @param list
	 * @return
	 * @author dengsq
	 */
	
	public Map<String,Object> submitAudit(List<EntProduct> list){
		 try {
			return entProductManager.submitAudit(list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	 
	public Map remove(List<String> idArray) throws Exception {
		String ids = StringUtil.getNewStrByList(idArray);
		EntProduct obj = new EntProduct();
		obj.setId(ids);
		obj.setUpdaterId(LoginSession.getUserid());
		return entProductManager.delete(obj,"true");
	}
	
	public List findProductListbyOrgId() {
		String orgId = LoginSession.getOrgid();
		try {
			Product obj = new Product();
			OrganizationService orgService = new OrganizationService();
			String orgIds = orgService.getChildrenNodeId(orgId);
			obj.setOrgId(StringUtils.isBlank(orgIds) ? "-1" : orgIds);
			List<Product> productList =ProductManager.getInstance().findProductListbyOrgId(obj);
			return productList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/*
	public Map saveAtt(Integer approveState,List listAtt) {
		try {
			Map map = new HashMap();
			map.put(ConstantUnit.O_PT, EnumOpt.Product_saveAtt.toString());
			EntProduct entProduct = new EntProduct();
			entProduct.setApproveState(approveState);
			map.put("entProduct", entProduct);
			map.put("list", listAtt);
			return ProductSocketClient.returnValue(map).getResult();
			
		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}
	}
	*/
	

}
